package Long.Lu.Service.Impl;


import Long.Lu.Dao.DeployDao;
import Long.Lu.Dao.EmployeeDao;
import Long.Lu.Service.DeployService;
import Long.Lu.pojo.Deploy;
import Long.Lu.pojo.DeployParam;
import Long.Lu.pojo.Employee;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: ty
 * @createDate: 2020/10/26
 * @version: 1.0
 */
@Service
public class DeployServiceImpl implements DeployService {
    @Autowired
    private DeployDao deployDao;

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<Deploy> SelectAllDeploy() {
        return deployDao.findAll();
    }

    @Override
    public boolean addDeploy(DeployParam deployParam) {
        System.out.println("====================");
        try {
            System.out.println("--------======");
            Deploy deploy = new Deploy();
            deploy.setTime(new Date());
            deploy.setRemarks(deployParam.getRemarks());
            deploy.setNewPost(deployParam.getNewPost());
            deploy.setEmployId(deployParam.getEmployId());
            deploy.setE_name(deployParam.getE_name());
            deploy.setBeforePost(deployParam.getBeforePost());
            System.out.println("----3333----======");
            deployDao.save(deploy);
            System.out.println("deploy=======>   "+deploy);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //删除一个员工
    @Override
    public boolean delDeploy(Integer deployId) {
        try {
            deployDao.deleteDeployByDeployId(deployId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Deploy updateById(Integer deployId) {
        return deployDao.findById(deployId).get();
    }

    @Override
    public boolean updateDeploy(DeployParam deployParam) {
        try {
            Deploy deploy = deployDao.findById(deployParam.getDeployId()).get();
            deploy.setE_name(deployParam.getE_name());
            deploy.setBeforePost(deployParam.getBeforePost());
            deploy.setNewPost(deployParam.getNewPost());
            deploy.setTime(deployParam.getTime());
            deploy.setRemarks(deployParam.getRemarks());

            deployDao.save(deploy);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Employee employeeById(Integer employeeId) {
        return employeeDao.findById(employeeId).get();
    }

    @Override
    public boolean delDeployAll(String deployIds) {
        try{
            if(deployIds!=null && !deployIds.equals("")) {
                String[] arrs = deployIds.split(",");
                deployDao.deleteAllDeployByIds(Arrays.asList(arrs));
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    //导出为excel
    @Override
    public void excelWrite(HttpServletResponse response) {

        OutputStream output=null;
        try {
            // 创建HSSFWorkbook对象 exlecl
            HSSFWorkbook wb = new HSSFWorkbook();
            // 创建HSSFSheet对象,如果要创建多个sheet，就再创建sheet对象
            HSSFSheet sheet = wb.createSheet("调配信息表");

            // 创建HSSFRow对象，先写入列名
            HSSFRow colName = sheet.createRow(0);
            // 写入入第一行列名
            colName.createCell(0).setCellValue("编号");
            colName.createCell(1).setCellValue("员工编号");
            colName.createCell(2).setCellValue("姓名");
            colName.createCell(3).setCellValue("原职务");
            colName.createCell(4).setCellValue("新职务");
            colName.createCell(5).setCellValue("调动时间");
            colName.createCell(6).setCellValue("调动原因");
            //查询员工所有信息  增阿分页功能 设置为0-10 并在finall().getContent();
            Pageable pageable = PageRequest.of(0,50);
            List<Deploy> list = deployDao.findAll(pageable).getContent();
            System.out.println("list==>"+list);
            for (int i = 1; i <=list.size(); i++) {
                //从第二行开始写入数据
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(list.get(i-1).getDeployId());
                row.createCell(1).setCellValue(list.get(i-1).getEmployId());
                row.createCell(2).setCellValue(list.get(i-1).getE_name());
                row.createCell(3).setCellValue(list.get(i-1).getBeforePost());
                row.createCell(4).setCellValue(list.get(i-1).getNewPost());
                row.createCell(5).setCellValue(list.get(i-1).getTime());
                row.createCell(6).setCellValue(list.get(i-1).getRemarks());
            }

            //输出Excel文件到页面
            output=response.getOutputStream();
            String fileName="调配信息表";
            //解决中文文件名下载 变成下划线的问题
            fileName=new String(fileName.getBytes("utf-8"),"ISO8859-1")+"";
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
            response.setContentType("application/msexcel");
            //文件下载
            wb.write(output);

        } catch (Exception e) {

            e.printStackTrace();
        }finally{
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
